package com.zpark.humpro.mapper;

import com.zpark.humpro.entity.Human;
import com.zpark.humpro.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据层实现类
 */
public class ManagerMapperImpl implements ManagerMapper {



    @Override
    public List<Human> selectAll() {

        Connection conn = JdbcUtil.getConnection();
        String sql = "select * from person_tb";
        try {
            PreparedStatement prepared = conn.prepareStatement(sql);
            //将查询的信息保存到结果集
            ResultSet resultSet = prepared.executeQuery();
            List<Human> humanList = new ArrayList<>();

            while (resultSet.next()){
                Human human = new Human();
                human.setName(resultSet.getString("name"));

            }


        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List<Human> selectByName(String name) {
        Connection conn = JdbcUtil.getConnection();
        String sql = "select * from person_tb where name = ?";
        //获取执行sql对象
        try {
            PreparedStatement prepared = conn.prepareStatement(sql);
            //sql赋值
            prepared.setString(1,name);
            //执行sql
            ResultSet resultSet = prepared.executeQuery();
            List<Human> humanList = new ArrayList<>();
            while(resultSet.next()){
                Human human = new Human();
                human.setId(resultSet.getInt("id"));
                human.setAge(resultSet.getInt("age"));
                human.setName(resultSet.getString("name"));
                human.setAdress(resultSet.getString("adress"));
                humanList.add(human);

            }
            return humanList;

        } catch (SQLException e) {
            //如果报错，在控制台打印语句
            e.printStackTrace();
        }


        return null;
    }
}
